﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Employees : System.Web.UI.Page
{
    protected LocalFunctions FUNC;
    DataTable DT;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin)this.Master).FUNC;

        lblPageName.Text = FUNC.GetPhrase(6);
        lblKeywords.Text = FUNC.GetPhrase(7);
        lblDescription.Text = FUNC.GetPhrase(8);

        GetData();
       

        // Get data from DB about page
        if (!IsPostBack)
        {
            GetPageData();
        }
    }

    private void GetPageData()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(5); // typeID - 1- Emoloyees
        DataTable DT = Dal.ExecuteDBsp("Admin_GetPageByID", Dal.arParams);

        // If not null - fill fields
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                DataRow Row = DT.Rows[0];

                txtPageTitle.Text = Row["PageTitle"].ToString();
                txtKeywords.Text = Row["Keywords"].ToString();
                txtDescription.Text = Row["Description"].ToString();

            }
    }

    protected void btnArticleSave_Click(object sender, EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(5); //Page ID

        Dal.arParams.Add(txtPageTitle.Text);
        Dal.arParams.Add(txtKeywords.Text);
        Dal.arParams.Add(txtDescription.Text);

        Dal.arParams.Add(string.Empty); //Title
        Dal.arParams.Add(string.Empty); //Content
        Dal.ExecuteDBsp("Admin_UpdatePageByID", Dal.arParams, true);
    }

    protected override void OnPreRender(EventArgs e)
    {

        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                dgEmployees.DataSource = DT;
                dgEmployees.DataBind();
            }
        base.OnPreRender(e);
    }

    private void GetData()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID); // Curent language
        DT = Dal.ExecuteDBsp("Admin_GetEmployees", Dal.arParams);
        // If not null - fill fields
       
    }

    protected void dgEmployees_ItemCommand(object source, CommandEventArgs e)
    {
        bool bChanged = false;
        Int32 rowID;
        Int32.TryParse(e.CommandArgument.ToString(), out rowID);
        Int32 rowIndex = 0;

        if (DT != null)
        {
            for (Int16 i = 0; i < DT.Rows.Count; i++)
            {
                if (DT.Rows[i]["ID"].Equals(rowID))
                {
                    rowIndex = i;
                    break;
                }
            }

            switch (e.CommandName)
            {
                case "MoveUp":
                    if (rowIndex > 0)
                    {
                        DT.Rows[rowIndex - 1]["orderID"] = rowIndex;
                        DT.Rows[rowIndex]["orderID"] = rowIndex - 1;
                        bChanged = true;
                    }
                    break;
                case "MoveDown":
                    if (rowIndex < DT.Rows.Count - 1)
                    {
                        DT.Rows[rowIndex + 1]["orderID"] = rowIndex;
                        DT.Rows[rowIndex]["orderID"] = rowIndex + 1;
                        bChanged = true;
                    }
                    break;
            }
            if (bChanged)
            {
                string sXml = string.Empty;
                for (int iCount = 0; iCount < DT.Rows.Count; iCount++)
                {
                    sXml += string.Format("<c id=\"{0}\" ord=\"{1}\"/>", DT.Rows[iCount]["ID"], DT.Rows[iCount]["orderID"]);
                }
                sXml = "<cs>" + sXml + "</cs>";

                GetData();

            }
        }
       
        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID); // Curent language
        DT = Dal.ExecuteDBsp("Admin_GetEmployees", Dal.arParams);
        //Session["FilesOrder"] = dt;
    }

    protected void dgEmployees_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgEmployees.EditIndex = e.NewEditIndex;
    }

    protected void dgEmployees_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgEmployees.EditIndex = -1;
    }

    protected void dgEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = dgEmployees.Rows[e.RowIndex];

        String sName = ((TextBox)row.FindControl("txtName")).Text;
        String sContent = ((TextBox)row.FindControl("txtContent")).Text;

        Int32 iID = Convert.ToInt32(((HiddenField)row.FindControl("hfID")).Value);

        FileUpload fUpload = ((FileUpload)row.FindControl("uplPhoto"));
        string sPicture = ((HiddenField)row.FindControl("hfFileName")).Value;

        if (fUpload.HasFile)
        {
            string sPath = Server.MapPath("~/content/galery");
            sPicture = "about_" + fUpload.FileName;
            fUpload.SaveAs(sPath+"\\"+ sPicture);
        }

        UpdateRecord(iID, sPicture, sName, sContent);
        dgEmployees.EditIndex = -1;
    }

    private void UpdateRecord(int iID,string sPicture,string sName,string sContent)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.arParams.Add(sPicture);   //Image
        Dal.arParams.Add(Helper.ConvertStringToSql(sName));
        Dal.arParams.Add(Helper.ConvertStringToSql(sContent));
        Dal.arParams.Add(FUNC.LangID);

        Dal.ExecuteDBsp("Admin_UpdateEmployees", Dal.arParams, true);
        
        GetData();
    }

    protected void dgEmployees_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = dgEmployees.Rows[e.RowIndex];

        Int32 iID = Convert.ToInt32(((HiddenField)row.FindControl("hfID")).Value);
        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.ExecuteDBsp("Admin_DeleteEmployee", Dal.arParams);
    }

    protected void btnFooterSave_Click(object sender, EventArgs e)
    {
        GridViewRow row = dgEmployees.FooterRow;

        String sName = ((TextBox)row.FindControl("txtName")).Text;
        String sContent = ((TextBox)row.FindControl("txtContent")).Text;

        FileUpload fUpload = ((FileUpload)row.FindControl("uplPhoto"));
        string sPicture = String.Empty ;

        if (fUpload.HasFile)
        {
            string sPath = Server.MapPath("~/content/galery");
            sPicture = "about_" + fUpload.FileName;
            fUpload.SaveAs(sPath + "\\" + sPicture);
        }

        UpdateRecord(0, sPicture, sName, sContent);
    }

    protected void dgEmployees_DataBinding(object sender, GridViewRowEventArgs e)
    {
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            cb.TargetControlID = e.Row.FindControl("btnDelete").ClientID.Replace("_", "$");
            cb.OnClientCancel = "CancelClick";
            cb.ConfirmText = "Вы уверены , что хотите удалить запись ?";
            e.Row.Cells[2].Controls.Add(cb);
        }
        catch { }
    }
}